using System;
using System.Collections.Generic;
using System.Text;
using Common.Shared.Design;

namespace Common.Shared.Xml.Wddx {
	[WddxRoot("recordset")]
	public class WddxRecordset : WddxDataTypeObject {
		private List<WddxRecordsetField> _fields;

		public WddxRecordset ( ) {
			this.Fields = new List<WddxRecordsetField> ( );
		}

		public string FieldNames {
			get {
				StringBuilder sb = new StringBuilder ( );
				foreach ( WddxRecordsetField field in this.Fields ) {
					sb.AppendFormat ( "{0},", field.Name );
				}
				if ( sb.Length > 1 ) {
					sb.Remove ( sb.Length - 1, 1 );
				}
				return sb.ToString ( );
			}
			set { return; }
		}

		[WddxAttribute("rowCount")]
		public int RowCount {
			get {
				if ( this.Fields.Count > 0 ) {
					return this.Fields[ 0 ].Count;
				} else {
					return 0;
				}
			}
			set { return; }
		}


		[WddxElement("field")]
		public List<WddxRecordsetField> Fields {
			get { return _fields; }
			set { _fields = value; }
		}

		public List<WddxPrimitiveDataTypeObject> GetRow ( int index ) {
			if ( index >= this.RowCount ) {
				return null;
			};

			List<WddxPrimitiveDataTypeObject> row = new List<WddxPrimitiveDataTypeObject> ( );
			foreach ( WddxRecordsetField field in this.Fields ) {
				row.Add ( field[ index ] );
			}
			return row;
		}

	}
}
